package com.open.rbac.controller.sys;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.open.center.common.model.CommonResult;
import com.open.rbac.model.dto.sys.RoleDto;
import com.open.rbac.model.vo.sys.RoleVo;
import com.open.rbac.model.vo.sys.group.RoleUpdate;
import com.open.rbac.service.sys.IRoleService;

/**
 * 角色控制器
 *
 * @author Riche's
 * @since 2022/8/26
 */

@Tag(name = "角色管理", description = "角色控制器")
@ApiResponses(value = {
    @ApiResponse(responseCode = "200", description = "成功",
        content = {@Content(schema = @Schema(implementation = RoleDto.class))}),
})
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/v1/sys/role")
public class RoleController {

    private final IRoleService roleService;

    @Operation(summary = "新增角色")
    @PostMapping()
    public CommonResult<RoleDto> create(@Valid @RequestBody RoleVo roleVo) {
        return CommonResult.success(roleService.create(roleVo));
    }

    @Operation(summary = "更新角色")
    @Validated(RoleUpdate.class)
    @PutMapping()
    public CommonResult<RoleDto> update(@Valid @RequestBody RoleVo roleVo) {
        return CommonResult.success(roleService.update(roleVo));
    }

    @Operation(summary = "角色信息")
    @GetMapping("/{id}")
    public CommonResult<RoleDto> get(@PathVariable String id) {
        return CommonResult.success(roleService.findById(id));
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteById(@PathVariable String id) {
        roleService.deleteById(id);
        return CommonResult.success();
    }


    @Operation(summary = "启用|禁用角色")
    @PutMapping("/enable/{id}")
    public CommonResult<Void> enable(@PathVariable String id,
                                     @RequestParam boolean enable) {
        if (enable) {
            roleService.enable(id);
        } else {
            roleService.disable(id);
        }
        return CommonResult.success();
    }

    @Operation(summary = "配置角色菜单关系")
    @PutMapping("/config/{roleId}")
    public CommonResult<Void> config(@PathVariable String roleId,
                                     @RequestBody
                                     @NotEmpty(message = "菜单ID列表为必选项")
                                     List<String> menuIds) {
        roleService.createMenuRels(roleId, menuIds);
        return CommonResult.success();
    }


    @Operation(summary = "按角色Id查询")
    @GetMapping("/by_id")
    public CommonResult<RoleDto> byId(@RequestParam String id) {
        RoleDto roleDto = roleService.findById(id);
        if (roleDto != null && roleDto.getId().equals(id)) {
            return CommonResult.success(null);
        }
        return CommonResult.success(roleDto);
    }

    @Operation(summary = "按角色名称查询")
    @GetMapping("/by_name")
    public CommonResult<RoleDto> byName(@RequestParam(required = false) String id,
                                        @RequestParam String name) {
        List<RoleDto> roleDtos = roleService.listByName(name);
        if (!roleDtos.isEmpty() && roleDtos.getFirst().getId().equals(id)) {
            return CommonResult.success(null);
        }
        return CommonResult.success(roleDtos.isEmpty() ? null : roleDtos.getFirst());
    }

    @Operation(summary = "角色列表")
    @PostMapping("/list")
    public CommonResult<List<RoleDto>> list(@RequestBody RoleVo roleVo) {
        return CommonResult.success(roleService.list(roleVo));
    }

}
